﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
using MSExcel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JLM2
{
    public partial class MainFrame : Form
    {
        /// <summary>
        /// 串口类实例
        /// </summary>
        JLMComm JLComm = new JLMComm();
        JLMConfig jlmConfig = new JLMConfig(@"jlm.config");
        JLMConfig testTypeConfig = new JLMConfig(@"jlm.last");
        JLMConfig btnLast = new JLMConfig(@"jlm.btn");
        JLMDataExchange dataExchange;
        JLMCommTaskRunService jlmTaskService;
        JLMPLCCommands jlCommand = new JLMPLCCommands(@"jlm.json");
        int charXCounter = 0;
        bool configZXC = true;
        string reportDir = "";
        bool serialPortOpened = false;
        bool closeByBtn = false;
        int cssj = 0;
        private string Reading_Interval { get; set; }

        Queue<float> jlmWWQueue = new Queue<float>();
        Queue<float> jlmWD3Queue = new Queue<float>();
        Queue<float> jlmNWQueue = new Queue<float>();
        Queue<float> jlmNWD4Queue = new Queue<float>();
        Queue<float> jlmNJQueue = new Queue<float>();
        Queue<String> jlmTimeQueue = new Queue<String>();

        int jlmQueueCounter = 0;

        protected bool OpenSerialPort()
        {
            return true;
        }

        /// <summary>
        /// 
        /// </summary>

        public MainFrame()
        {
            InitializeComponent();
            //// 16位的十六进制数
            //string highPartHex = "49F0";
            //string lowPartHex = "0002";

            //var sf = Convert.ToString(Convert.ToInt32(highPartHex, 16) + 1, 16);

            //int bb = 18928;
            //var kk = Convert.ToString(bb, 16);

            //// 将十六进制字符串转换为16位的整数
            //ushort highPart = Convert.ToUInt16(highPartHex, 16);
            //ushort lowPart = Convert.ToUInt16(lowPartHex, 16);

            //var aaa = Convert.ToUInt32(highPart);

            //// 将两个16位的整数合并成一个32位的整数
            //uint combinedValue = (uint)(highPart << 16) | lowPart;

            //// 将32位的整数转换为十进制数
            //int decimalValue = (int)combinedValue;


            //var aaa = "010304FBD000012C".Substring(7, 4);
            //string hexString = "D000012C";
            //// 将十六进制字符串转换为整数
            //int intValue = int.Parse(aaa, System.Globalization.NumberStyles.HexNumber);
            //// 取低8位
            //int low8Bits = intValue & 0xFF;
            //float yy = intValue;
            //var result = yy /= 10.0f;
            // 十六进制数
            string hexNumber = "11b0"; //D432

            // 将十六进制数转换为十进制数
            int decimalNumber = Convert.ToInt32(hexNumber, 16);

            // 十进制数
            int decimalSubtract = 118;

            // 进行减法运算
            int resultDecimal = decimalNumber + decimalSubtract;

            // 将结果转换回十六进制数
            string resultHex = resultDecimal.ToString("X");

            #region 20241106 加载读取间隔配置文件
            if (!File.Exists($"jlm_Interval.config"))
            {
                FileStream config_IntervalFile = new FileStream(@"jlm_Interval.config", FileMode.Create);
                byte[] buffer1 = Encoding.ASCII.GetBytes("2");
                config_IntervalFile.Write(buffer1, 0, buffer1.Length);
                config_IntervalFile.Flush();
                config_IntervalFile.Close();
                this.Reading_Interval = "2";
            }
            else
            {
                JLMConfig jlm_Interval = new JLMConfig(@"jlm_Interval.config");
                this.Reading_Interval = jlm_Interval.ConfigParam;
            }
            #endregion

            if (!JLComm.Open(jlmConfig.ConfigParam))
            {
                MessageBox.Show("打开串口失败，请检查串口设定！");
                serialPortOpened = false;
            }
            else
            {
                serialPortOpened = true;
            }

            JLMLog.Open(@"jlm.log", true);

            // 采集报告目录
            Directory.CreateDirectory(Application.StartupPath + "\\reports");
            btnGBMS.BackColor = Color.LightGreen;

            reportDir = Application.StartupPath + "\\reports\\";
            Directory.CreateDirectory(reportDir);

            // 可变资源
            StreamReader file = File.OpenText(@"res\res.json");
            JsonTextReader reader = new JsonTextReader(file);
            JObject jsonObject = (JObject)JToken.ReadFrom(reader);
            labelCaption.Text = jsonObject["caption"].ToString();
            labelCompany.Text = jsonObject["company"].ToString();
            if (File.Exists(@"res\barcode.jpg"))
            {
                labelBarcode.Image = Image.FromFile(@"res\barcode.jpg");
            }
            else
            {
                labelBarcode.Visible = false;
            }

            if (File.Exists(@"res\product.jpg"))
            {
                labelProductImage.Image = Image.FromFile(@"res\product.jpg");
            }
            else
            {
                labelProductImage.Visible = false;
            }

            if (testTypeConfig.ConfigParam == null)
            {
                btnZXC.BackColor = Color.LightGreen;
                btnTime.BackColor = Color.LightGray;
                configZXC = true;

                string cmd = "01 05 0A 06";
                JLComm.ControlBtn(cmd, true);

                if (File.Exists(@"jlm.last"))
                    File.Delete(@"jlm.last");
                FileStream configFile = new FileStream(@"jlm.last", FileMode.Create);

                //开始写入
                byte[] buffer = Encoding.ASCII.GetBytes("zxc");
                configFile.Write(buffer, 0, buffer.Length);
                configFile.Flush();
                configFile.Close();
            }
            else
            {
                if (string.Compare(testTypeConfig.ConfigParam, "zxc") == 0)
                {
                    btnZXC.BackColor = Color.LightGreen;
                    btnTime.BackColor = Color.LightGray;
                    configZXC = true;
                    string cmd = "01 05 0A 06";
                    JLComm.ControlBtn(cmd, true);
                }
                else
                {
                    btnZXC.BackColor = Color.LightGray;
                    btnTime.BackColor = Color.LightGreen;
                    configZXC = false;
                    string cmd = "01 05 0A 07";
                    JLComm.ControlBtn(cmd, true);
                }
            }

            // buttons
            if (btnLast.ConfigParam == null)
            {
                if (File.Exists(@"jlm.btn"))
                    File.Delete(@"jlm.btn");
                FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

                //开始写入
                byte[] buffer = Encoding.ASCII.GetBytes("GBMS");
                configFile.Write(buffer, 0, buffer.Length);
                configFile.Flush();
                configFile.Close();

                JLComm.ControlBtn(jlCommand.yxBtnGBMS, true);
                btnGBMS.BackColor = Color.LightGreen;
                btnOBMS.BackColor = Color.LightGray;
                btnMBMS.BackColor = Color.LightGray;
                btnAMS.BackColor = Color.LightGray;
                btnBMS.BackColor = Color.LightGray;
                btnCMS.BackColor = Color.LightGray;
                btnDMS.BackColor = Color.LightGray;
                btnEMS.BackColor = Color.LightGray;
                btnFMS.BackColor = Color.LightGray;
            }
            else
            {
                if (string.Compare(btnLast.ConfigParam, "GBMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnGBMS, true);
                    btnGBMS.BackColor = Color.LightGreen;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "OBMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnOBMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGreen;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "MBMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnMBMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGreen;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "AMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnAMS, true);
                    btnAMS.BackColor = Color.LightGreen;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "BMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnBMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGreen;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "CMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnCMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGreen;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "DMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnDMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGreen;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "EMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnEMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGreen;
                    btnFMS.BackColor = Color.LightGray;
                }
                else if (string.Compare(btnLast.ConfigParam, "FMS") == 0)
                {
                    JLComm.ControlBtn(jlCommand.yxBtnFMS, true);
                    btnGBMS.BackColor = Color.LightGray;
                    btnOBMS.BackColor = Color.LightGray;
                    btnMBMS.BackColor = Color.LightGray;
                    btnAMS.BackColor = Color.LightGray;
                    btnBMS.BackColor = Color.LightGray;
                    btnCMS.BackColor = Color.LightGray;
                    btnDMS.BackColor = Color.LightGray;
                    btnEMS.BackColor = Color.LightGray;
                    btnFMS.BackColor = Color.LightGreen;
                }
            }

            if (serialPortOpened)
            {
                dataExchange = new JLMDataExchange();
                jlmTaskService = new JLMCommTaskRunService(dataExchange);
                jlmTaskService.Start(JLComm);
                jlmTaskService.SetReadAll(true);

                timerRead.Interval = 1200;
                timerRead.Start();
            }

            //20241030源
            this.LoadWeightData();
        }

        #region 20241030源增加

        /// <summary>
        /// 加载KG数据
        /// </summary>
        private void LoadWeightData()
        {
            Thread myThread = new Thread(running =>
            {
                while (true)
                {
                    if (serialPortOpened)
                    {
                        JLMBLSetParam param = new JLMBLSetParam();
                        JLComm.ReadIndexBLCSSZParam2(param);
                        this.Invoke(new Action(() =>
                        {
                            textYL1.Text = param.BL1;
                            textYL2.Text = param.BL2;
                            textYL3.Text = param.BL3;
                            textYL4.Text = param.BL4;
                            textYL5.Text = param.BL5;
                            textYL6.Text = param.BL6;
                            textYL7.Text = param.BL7;
                            textYL8.Text = param.BL8;
                            textYL9.Text = param.BL9;
                            //textYL10.Text = param.BL10;
                        }));
                        break;
                    }
                    else Thread.Sleep(2000);
                }
            });
            myThread.IsBackground = false;
            myThread.Start();
        }

        #endregion

        private void timerRead_Tick(object sender, EventArgs e)
        {
            try
            {
                textBH.Text = dataExchange.BH;
                textMC.Text = dataExchange.MC;
                textTH.Text = dataExchange.TH;
                textWJ.Text = dataExchange.WJ;
                textYD.Text = dataExchange.YD;
                textCL.Text = dataExchange.CL;

                textZXC.Text = dataExchange.ZXC;
                textSJ.Text = dataExchange.SJ;
                textZACS.Text = dataExchange.ZACS;
                textCSFZ.Text = dataExchange.CSFZ;
                textCSSD.Text = dataExchange.CSSD;
                textZAWGD.Text = dataExchange.ZAWGD;

                textZZSJ.Text = dataExchange.ZZ;
                textFZSJ.Text = dataExchange.FZ;
                textJXTD.Text = dataExchange.JXTD;

                // textBH.Text = dataExchange.BH;
                int value = dataExchange.JQZT;
                //Utils.WriteLog(this.GetType().ToString(), "Value->" + value + Environment.NewLine + JsonConvert.SerializeObject(dataExchange));
                if (value == 1)
                {
                    textJQZT.Text = "待机中";
                    cssj = dataExchange.maxCSSJ;
                }
                else if (value == 2)
                {
                    textJQZT.Text = "正转中";
                    cssj = dataExchange.maxCSSJ;
                }
                else if (value == 3)
                {
                    textJQZT.Text = "反转中";
                    cssj = dataExchange.maxCSSJ;
                }
                else if (value == 4)
                {
                    textJQZT.Text = "停顿中";
                    cssj = dataExchange.maxCSSJ;
                }
                else if (value == 5)
                {
                    textJQZT.Text = "测试完毕";
                }
                textXC.Text = dataExchange.XC;
                textPJSD.Text = dataExchange.PJSD;
                textXZSD.Text = dataExchange.XZSD;
                textCSSJ.Text = dataExchange.CSSJ;
                textPZCS.Text = dataExchange.PZCS;
                textLZBX.Text = dataExchange.LZBX;
                textDJNJ.Text = dataExchange.DJNJ;
                textWD2.Text = dataExchange.NWD;
                textDJZS.Text = dataExchange.DJZS;

                float fv1 = 1.0f, fv2 = 1.0f;
                fv2 = fv1 = dataExchange.WD;
                if (fv1 > 0)
                {
                    fv2 = fv1 / 10.0f;
                }
                textWD1.Text = fv2.ToString();
                chartWD.Series[0].Points.AddXY(charXCounter, fv2);
                charXCounter += 1;

                textWD3.Text = dataExchange.WD3;
                textWD4.Text = dataExchange.NWD4;

                jlmTaskService.SetReadAll(false);

                // 停止本计时器
                timerRead.Stop();

                // 启动读取机器状态定时器
                timerTask.Interval = int.Parse(this.Reading_Interval) * 1000;
                timerTask.Start();
            }
            catch (Exception ex)
            {
                Utils.WriteLog(this.GetType().ToString(), "timerRead_Tick=>" + ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        private void timerTask_Tick(object sender, EventArgs e)
        {
            try
            {
                float fv1 = 0.0f, fv2 = 0.0f;
                if (checkBoxRefreshAllData.Checked)
                {
                    jlmTaskService.SetReadAll(true);

                    textBH.Text = dataExchange.BH;
                    textWJ.Text = dataExchange.WJ;
                    textYD.Text = dataExchange.YD;
                    //Utils.WriteLog(this.GetType().ToString(), $"BH={dataExchange.BH},WJ={dataExchange.WJ},YD={dataExchange.YD},MC={dataExchange.MC}{dataExchange.MC.Trim().Length},TH={dataExchange.TH}{dataExchange.TH.Trim().Length},CL={dataExchange.CL}{dataExchange.CL.Trim().Length};");
                    if (dataExchange.MC.Trim().Length > 8)
                    {
                        //textMC.Text = dataExchange.MC.Trim().Remove('&').Substring(0, 8);
                        textMC.Text = dataExchange.MC.Trim();
                    }
                    else
                    {
                        textMC.Text = dataExchange.MC.Trim().Remove('&');
                    }

                    if (dataExchange.TH.Trim().Length > 8)
                    {
                        //textTH.Text = dataExchange.TH.Trim().Remove('&').Substring(0, 8);
                        textTH.Text = dataExchange.TH.Trim(); ;
                    }
                    else
                    {
                        textTH.Text = dataExchange.TH.Trim().Remove('&');
                    }

                    if (dataExchange.CL.Trim().Length > 8)
                    {
                        //textCL.Text = dataExchange.CL.Trim().Remove('&').Substring(0, 8);
                        textCL.Text = dataExchange.CL.Trim();
                    }
                    else
                    {
                        textCL.Text = dataExchange.CL.Trim().Remove('&');
                    }


                    textZXC.Text = dataExchange.ZXC;
                    textSJ.Text = dataExchange.SJ;
                    textZACS.Text = dataExchange.ZACS;
                    textCSFZ.Text = dataExchange.CSFZ;
                    textCSSD.Text = dataExchange.CSSD;
                    textZAWGD.Text = dataExchange.ZAWGD;

                    textZZSJ.Text = dataExchange.ZZ;
                    textFZSJ.Text = dataExchange.FZ;
                    textJXTD.Text = dataExchange.JXTD;
                }
                else
                {
                    jlmTaskService.SetReadAll(false);
                }

                int value = dataExchange.JQZT;
                //Utils.WriteLog(this.GetType().ToString(), "Timer2->" + value + Environment.NewLine + JsonConvert.SerializeObject(dataExchange));
                if (value == 1)
                {
                    textJQZT.Text = "待机中";
                }
                else if (value == 2)
                {
                    textJQZT.Text = "正转中";
                    fv2 = dataExchange.WD;
                    if (fv2 > 0)
                    {
                        fv2 = fv2 / 10.0f;
                    }

                    jlmQueueCounter += 1;
                    if (jlmQueueCounter >= 10)
                    {
                        if (jlmWWQueue.Count >= 2000)
                        {
                            jlmWWQueue.Dequeue();
                        }
                        jlmWWQueue.Enqueue(Convert.ToSingle(fv2));
                        if (jlmNWQueue.Count >= 2000)
                        {
                            jlmNWQueue.Dequeue();
                        }

                        jlmWD3Queue.Enqueue(Convert.ToSingle(dataExchange.WD3));
                        if (jlmWD3Queue.Count >= 2000)
                        {
                            jlmWD3Queue.Dequeue();
                        }

                        jlmNWQueue.Enqueue(Convert.ToSingle(dataExchange.NWD));
                        if (jlmNJQueue.Count >= 2000)
                        {
                            jlmNJQueue.Dequeue();
                        }

                        jlmNWD4Queue.Enqueue(Convert.ToSingle(dataExchange.NWD4));
                        if (jlmNWD4Queue.Count >= 2000)
                        {
                            jlmNWD4Queue.Dequeue();
                        }

                        jlmNJQueue.Enqueue(Convert.ToSingle(dataExchange.DJNJ));

                        String time = DateTime.Now.ToLocalTime().ToString("s");
                        jlmTimeQueue.Enqueue(time);

                        jlmQueueCounter = 0;
                    }
                }
                else if (value == 3)
                {
                    textJQZT.Text = "反转中";
                    fv2 = dataExchange.WD;
                    if (fv2 > 0)
                    {
                        fv2 = fv2 / 10.0f;
                    }

                    jlmQueueCounter += 1;
                    if (jlmQueueCounter >= 10)
                    {
                        if (jlmWWQueue.Count >= 2000)
                        {
                            jlmWWQueue.Dequeue();
                        }
                        jlmWWQueue.Enqueue(Convert.ToSingle(fv2));
                        if (jlmNWQueue.Count >= 2000)
                        {
                            jlmNWQueue.Dequeue();
                        }

                        jlmWD3Queue.Enqueue(Convert.ToSingle(dataExchange.WD3));
                        if (jlmWD3Queue.Count >= 2000)
                        {
                            jlmWD3Queue.Dequeue();
                        }

                        jlmNWQueue.Enqueue(Convert.ToSingle(dataExchange.NWD));
                        if (jlmNJQueue.Count >= 2000)
                        {
                            jlmNJQueue.Dequeue();
                        }

                        jlmNWD4Queue.Enqueue(Convert.ToSingle(dataExchange.NWD4));
                        if (jlmNWD4Queue.Count >= 2000)
                        {
                            jlmNWD4Queue.Dequeue();
                        }

                        jlmNJQueue.Enqueue(Convert.ToSingle(dataExchange.DJNJ));
                        String time = DateTime.Now.ToLocalTime().ToString("s");
                        jlmTimeQueue.Enqueue(time);

                        jlmQueueCounter = 0;
                    }

                }
                else if (value == 4)
                {
                    textJQZT.Text = "停顿中";
                    fv2 = dataExchange.WD;
                    if (fv2 > 0)
                    {
                        fv2 = fv2 / 10.0f;
                    }
                    jlmQueueCounter += 1;
                    if (jlmQueueCounter >= 10)
                    {
                        if (jlmWWQueue.Count >= 2000)
                        {
                            jlmWWQueue.Dequeue();
                        }
                        jlmWWQueue.Enqueue(Convert.ToSingle(fv2));
                        if (jlmNWQueue.Count >= 2000)
                        {
                            jlmNWQueue.Dequeue();
                        }

                        jlmWD3Queue.Enqueue(Convert.ToSingle(dataExchange.WD3));
                        if (jlmWD3Queue.Count >= 2000)
                        {
                            jlmWD3Queue.Dequeue();
                        }

                        jlmNWQueue.Enqueue(Convert.ToSingle(dataExchange.NWD));
                        if (jlmNJQueue.Count >= 2000)
                        {
                            jlmNJQueue.Dequeue();
                        }

                        jlmNWD4Queue.Enqueue(Convert.ToSingle(dataExchange.NWD4));
                        if (jlmNWD4Queue.Count >= 2000)
                        {
                            jlmNWD4Queue.Dequeue();
                        }

                        jlmNJQueue.Enqueue(Convert.ToSingle(dataExchange.DJNJ));
                        String time = DateTime.Now.ToLocalTime().ToString("s");
                        jlmTimeQueue.Enqueue(time);

                        jlmQueueCounter = 0;
                    }
                }
                else if (value == 5)
                {
                    textJQZT.Text = "测试完毕";
                    fv2 = dataExchange.WD;
                    if (fv2 > 0)
                    {
                        fv2 = fv2 / 10.0f;
                    }

                    jlmQueueCounter += 1;
                    if (jlmQueueCounter >= 10)
                    {
                        if (jlmWWQueue.Count >= 2000)
                        {
                            jlmWWQueue.Dequeue();
                        }
                        jlmWWQueue.Enqueue(Convert.ToSingle(fv2));
                        if (jlmNWQueue.Count >= 2000)
                        {
                            jlmNWQueue.Dequeue();
                        }

                        jlmWD3Queue.Enqueue(Convert.ToSingle(dataExchange.WD3));
                        if (jlmWD3Queue.Count >= 2000)
                        {
                            jlmWD3Queue.Dequeue();
                        }

                        jlmNWQueue.Enqueue(Convert.ToSingle(dataExchange.NWD));
                        if (jlmNJQueue.Count >= 2000)
                        {
                            jlmNJQueue.Dequeue();
                        }

                        jlmNWD4Queue.Enqueue(Convert.ToSingle(dataExchange.NWD4));
                        if (jlmNWD4Queue.Count >= 2000)
                        {
                            jlmNWD4Queue.Dequeue();
                        }

                        jlmNJQueue.Enqueue(Convert.ToSingle(dataExchange.DJNJ));
                        String time = DateTime.Now.ToLocalTime().ToString("s");
                        jlmTimeQueue.Enqueue(time);

                        jlmQueueCounter = 0;
                    }

                }

                textXC.Text = dataExchange.XC;
                textPJSD.Text = dataExchange.PJSD;
                textXZSD.Text = dataExchange.XZSD;
                textCSSJ.Text = dataExchange.CSSJ;
                textPZCS.Text = dataExchange.PZCS;
                textLZBX.Text = dataExchange.LZBX;
                textDJNJ.Text = dataExchange.DJNJ;
                textWD2.Text = dataExchange.NWD;
                textDJZS.Text = dataExchange.DJZS;

                fv2 = fv1 = dataExchange.WD;
                if (fv1 > 0)
                {
                    fv2 = fv1 / 10.0f;
                }
                textWD1.Text = fv2.ToString();
                chartWD.Series[0].Points.AddXY(charXCounter, fv2);

                textWD3.Text = dataExchange.WD3;
                textWD4.Text = dataExchange.NWD4;

                charXCounter += 1;
            }
            catch (Exception ex)
            {
                Utils.WriteLog(this.GetType().ToString(), "timerTask_Tick->" + ex.Message);
                MessageBox.Show("timerTask_Tick" + ex.Message);
            }
        }

        #region 按钮事件
        private void btnZXC_Click(object sender, EventArgs e)
        {
            string cmd = "01 05 0A 06";
            JLComm.ControlBtn(cmd, true);
            btnZXC.BackColor = Color.LightGreen;
            btnTime.BackColor = Color.LightGray;
            configZXC = true;

            if (File.Exists(@"jlm.last"))
                File.Delete(@"jlm.last");
            FileStream configFile = new FileStream(@"jlm.last", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("zxc");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnTime_Click(object sender, EventArgs e)
        {
            string cmd = "01 05 0A 07";
            JLComm.ControlBtn(cmd, true);
            btnZXC.BackColor = Color.LightGray;
            btnTime.BackColor = Color.LightGreen;
            configZXC = false;

            if (File.Exists(@"jlm.last"))
                File.Delete(@"jlm.last");
            FileStream configFile = new FileStream(@"jlm.last", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("sj");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void btnGBMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnGBMS, true);
            btnGBMS.BackColor = Color.LightGreen;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("GBMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnOBMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnOBMS, true);
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGreen;
            btnMBMS.BackColor = Color.LightGray;
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("OBMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnMBMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnMBMS, true);
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGreen;
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("MBMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();

        }

        private void btnAMS_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(DateTime.Now.ToLocalTime().ToString() + "." + DateTime.Now.Millisecond.ToString());
            JLComm.ControlBtn(jlCommand.yxBtnAMS, true);
            btnAMS.BackColor = Color.LightGreen;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;
            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("AMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnBMS_Click(object sender, EventArgs e)
        {
            JLMTestModeData modeData = new JLMTestModeData();
            modeData.ZZ = textZZSJ.Text;
            modeData.FZ = textFZSJ.Text;
            modeData.JXTD = textJXTD.Text;
            JLComm.WriteTestModeParam(modeData);

            JLComm.ControlBtn(jlCommand.yxBtnBMS, true);
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGreen;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("BMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnCMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnCMS, true);
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGreen;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("CMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnDMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnDMS, true);
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGreen;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGray;
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("DMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private void btnEMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnEMS, true);
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGreen;
            btnFMS.BackColor = Color.LightGray;
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("EMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();


        }

        private void btnFMS_Click(object sender, EventArgs e)
        {
            JLComm.ControlBtn(jlCommand.yxBtnFMS, true);
            btnAMS.BackColor = Color.LightGray;
            btnBMS.BackColor = Color.LightGray;
            btnCMS.BackColor = Color.LightGray;
            btnDMS.BackColor = Color.LightGray;
            btnEMS.BackColor = Color.LightGray;
            btnFMS.BackColor = Color.LightGreen;
            btnGBMS.BackColor = Color.LightGray;
            btnOBMS.BackColor = Color.LightGray;
            btnMBMS.BackColor = Color.LightGray;

            JLMTestModeData mode = new JLMTestModeData();
            JLComm.ReadTestModeParam(mode);
            textZZSJ.Text = mode.ZZ;
            textFZSJ.Text = mode.FZ;
            textJXTD.Text = mode.JXTD;

            if (File.Exists(@"jlm.btn"))
                File.Delete(@"jlm.btn");
            FileStream configFile = new FileStream(@"jlm.btn", FileMode.Create);

            //开始写入
            byte[] buffer = Encoding.ASCII.GetBytes("FMS");
            configFile.Write(buffer, 0, buffer.Length);
            configFile.Flush();
            configFile.Close();
        }

        private bool CheckParam()
        {
            if (textBH.Text.Length == 0)
                return false;
            if (textMC.Text.Length == 0 || textMC.Text.Length > 10)
                return false;
            if (textTH.Text.Length == 0 || textTH.Text.Length > 10)
                return false;
            if (textWJ.Text.Length == 0)
                return false;
            if (textYD.Text.Length == 0)
                return false;
            if (textCL.Text.Length == 0 || textCL.Text.Length > 10)
                return false;

            if (configZXC)
            {
                if (textZXC.Text.Length == 0)
                    return false;
            }
            else if (textSJ.Text.Length == 0)
            {
                return false;
            }

            if (textZACS.Text.Length == 0)
            {
                return false;
            }

            if (textCSFZ.Text.Length == 0)
            {
                return false;
            }
            if (textCSSD.Text.Length == 0)
            {
                return false;
            }
            if (textZAWGD.Text.Length == 0)
            {
                return false;
            }
            return true;
        }

        private void btnStartTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckParam())
                {
                    MessageBox.Show("请确认输入参数！");
                    return;
                }

                // 清空队列中保存的数据
                jlmWWQueue.Clear();
                jlmWD3Queue.Clear();
                jlmNWQueue.Clear();
                jlmNWD4Queue.Clear();
                jlmNJQueue.Clear();
                jlmTimeQueue.Clear();

                chartWD.Series[0].Points.Clear();
                JLMProductData productData = new JLMProductData();
                productData.BH = textBH.Text;
                productData.MC = textMC.Text;
                productData.TH = textTH.Text;
                productData.WJ = textWJ.Text;
                productData.YD = textYD.Text;
                productData.CL = textCL.Text;
                JLMTestParamData testData = new JLMTestParamData();
                testData.configZXC = configZXC;
                testData.ZXC = textZXC.Text;
                testData.SJ = textSJ.Text;
                testData.ZACS = textZACS.Text;
                testData.CSFZ = textCSFZ.Text;
                testData.CSSD = textCSSD.Text;
                testData.ZAWGD = textZAWGD.Text;
                JLComm.WriteTestParam(productData, testData);
                Thread.Sleep(20);
                JLComm.ControlBtn(jlCommand.yxBtnKSCS, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPauseTest_Click(object sender, EventArgs e)
        {
            string cmd = "01 05 08 c1";
            JLComm.ControlBtn(cmd, true);
        }

        private void btnStopTest_Click(object sender, EventArgs e)
        {
            chartWD.Series[0].Points.Clear();
            JLComm.ControlBtn(jlCommand.yxBtnTZCS, true);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            closeByBtn = true;
            timerTask.Stop();
            string cmd = "01 05 08 c0"; // m192
            if (serialPortOpened)
            {
                JLComm.ControlBtn(cmd, true);
                jlmTaskService.Stop();
            }

            Close();
            // 强制退出
            Environment.Exit(0);
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            Setting setDiag = new Setting();
            setDiag.SetParam(JLComm, jlCommand);
            setDiag.ShowDialog();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            ReportList listFrm = new ReportList();
            listFrm.ShowDialog();
            return;
        }

        private void btnSaveData_Click(object sender, EventArgs e)
        {
            JLMTestModeData modeData = new JLMTestModeData();
            modeData.ZZ = textZZSJ.Text;
            modeData.FZ = textFZSJ.Text;
            modeData.JXTD = textJXTD.Text;
            JLComm.WriteTestModeParam(modeData);
            JLComm.ControlBtn(jlCommand.yxBtnBCSJ, true);

            JLMBLSetParam BLSetParam = new JLMBLSetParam();
            BLSetParam.BL1 = textYL1.Text;
            BLSetParam.BL2 = textYL2.Text;
            BLSetParam.BL3 = textYL3.Text;
            BLSetParam.BL4 = textYL4.Text;
            BLSetParam.BL5 = textYL5.Text;
            BLSetParam.BL6 = textYL6.Text;
            BLSetParam.BL7 = textYL7.Text;
            BLSetParam.BL8 = textYL8.Text;
            BLSetParam.BL9 = textYL9.Text;
            if (!string.IsNullOrEmpty(BLSetParam.BL1)) JLComm.Write32BitParam(BLSetParam.BL1, "11 b8");
            if (!string.IsNullOrEmpty(BLSetParam.BL2)) JLComm.Write32BitParam(BLSetParam.BL2, "11 ba");
            if (!string.IsNullOrEmpty(BLSetParam.BL3)) JLComm.Write32BitParam(BLSetParam.BL3, "11 bc");
            if (!string.IsNullOrEmpty(BLSetParam.BL4)) JLComm.Write32BitParam(BLSetParam.BL4, "11 be");
            if (!string.IsNullOrEmpty(BLSetParam.BL5)) JLComm.Write32BitParam(BLSetParam.BL5, "11 c0");
            if (!string.IsNullOrEmpty(BLSetParam.BL6)) JLComm.Write32BitParam(BLSetParam.BL6, "11 c2");
            if (!string.IsNullOrEmpty(BLSetParam.BL7)) JLComm.Write32BitParam(BLSetParam.BL7, "11 c4");
            if (!string.IsNullOrEmpty(BLSetParam.BL8)) JLComm.Write32BitParam(BLSetParam.BL8, "11 c6");
            if (!string.IsNullOrEmpty(BLSetParam.BL9)) JLComm.Write32BitParam(BLSetParam.BL9, "11 c8");
            MessageBox.Show("配置已保存！");
        }
        #endregion

        private void SaveReport()
        {
            JLMTJParam param = new JLMTJParam();
            JLComm.ReadTJParam(param);

            try
            {
                string dir = Application.StartupPath;
                MSExcel.Application app = new MSExcel.Application();
                MSExcel.Workbooks wbks = app.Workbooks;
                app.Visible = false;

                string templateFile = dir + "\\report.xls";
                MSExcel._Workbook _wbk = wbks.Add(templateFile);
                MSExcel.Sheets shs = _wbk.Sheets;
                MSExcel._Worksheet _wsh = (MSExcel._Worksheet)shs.get_Item(1);

                //获得单元格对象
                _wsh.Cells[5, 8] = textBH.Text;  // 编号
                _wsh.Cells[14, 2] = textMC.Text;  // 名称
                _wsh.Cells[14, 7] = textTH.Text;  // 图号

                _wsh.Cells[17, 2] = textWJ.Text;  // 外径
                _wsh.Cells[17, 6] = textYD.Text;  // 硬度
                _wsh.Cells[17, 8] = textCL.Text;  // 材料

                _wsh.Cells[22, 3] = textCSSD.Text;  // 速度
                _wsh.Cells[22, 8] = textCSFZ.Text;  // 负载

                _wsh.Cells[25, 3] = textZAWGD.Text;  // 障碍物高度
                _wsh.Cells[25, 8] = textPZCS.Text;  // 碰撞次数

                _wsh.Cells[28, 3] = textXC.Text;  // 总行程
                _wsh.Cells[22, 8] = textCSSJ.Text;  // 测试时间                
                _wsh.Cells[28, 8] = textDJZS.Text;  // 电机转数

                //_wsh.Cells[31, 8] = param.TJLMWD;  // 停机时轮面温度
                _wsh.Cells[34, 8] = textWD1.Text; //温度1
                _wsh.Cells[37, 8] = textWD2.Text; //温度2
                _wsh.Cells[40, 8] = textWD3.Text; //温度3
                _wsh.Cells[43, 8] = textWD4.Text; //温度4

                _wsh.Cells[31, 8] = textDJNJ.Text; //扭距力
                _wsh.Cells[31, 3] = textLZBX.Text; //轮子型变

                _wsh.Cells[36, 4] = textZZSJ.Text + "min(分)";  // 正转时间
                _wsh.Cells[36, 7] = textJXTD.Text + "min(分)";  // 停顿时间

                _wsh.Cells[69, 8] = DateTime.Now.ToLocalTime().ToString();   // 停顿时间

                JLMBLSetParam YLParams = new JLMBLSetParam();
                JLComm.ReadIndexBLCSSZParam2(YLParams);
                _wsh.Cells[49, 2] = YLParams.BL1; _wsh.Cells[49, 6] = YLParams.BL2; _wsh.Cells[49, 8] = YLParams.BL3;
                _wsh.Cells[51, 2] = YLParams.BL4; _wsh.Cells[51, 6] = YLParams.BL5; _wsh.Cells[51, 8] = YLParams.BL6;
                _wsh.Cells[53, 2] = YLParams.BL7; _wsh.Cells[53, 6] = YLParams.BL8; _wsh.Cells[53, 8] = YLParams.BL9;

                JLMTSYMParam settingYLEx = new JLMTSYMParam();
                JLComm.ReadTSYMParam(settingYLEx);
                _wsh.Cells[34, 3] = settingYLEx.SZYL1; //设置压力1
                _wsh.Cells[37, 3] = settingYLEx.SZYL2; //设置压力2
                _wsh.Cells[40, 3] = settingYLEx.SZYL3; //设置压力3
                _wsh.Cells[43, 3] = settingYLEx.SZYL4; //设置压力4

                if (textFZSJ.Text.Length > 0)
                {
                    if (textFZSJ.Text != "0")
                    {
                        _wsh.Cells[46, 8] = "☑正反转";   // 
                    }
                }

                string[] timeArray = new string[jlmTimeQueue.Count + 16];
                int counter = 0;
                while (jlmTimeQueue.Count > 0)
                {
                    // timeArray[counter] = jlmTimeQueue.Dequeue().ToString("G");
                    timeArray[counter] = jlmTimeQueue.Dequeue();
                    counter += 1;
                }
                //Utils.WriteLog(this.GetType().ToString(), $"jlmWWQueue={jlmWWQueue.Count};jlmNWQueue={jlmNWQueue.Count};jlmWD3Queue={jlmWD3Queue.Count};jlmNWD4Queue={jlmNWD4Queue.Count};jlmNJQueue={jlmNJQueue.Count};");
                // 温度1
                counter = 0;
                var SheetWD1 = (MSExcel._Worksheet)shs.get_Item("温度一");
                if (SheetWD1 != null)
                {
                    while (jlmWWQueue.Count > 0)
                    {
                        counter += 1;
                        SheetWD1.Cells[counter, 1] = timeArray[counter - 1];
                        SheetWD1.Cells[counter, 2] = jlmWWQueue.Dequeue().ToString();
                    }
                    jlmWWQueue.Clear();
                }

                // 温度2                
                counter = 0;
                var SheetWD2 = (MSExcel._Worksheet)shs.get_Item("温度二");
                if (SheetWD2 != null)
                {
                    while (jlmNWQueue.Count > 0)
                    {
                        counter += 1;
                        SheetWD2.Cells[counter, 1] = timeArray[counter - 1];
                        SheetWD2.Cells[counter, 2] = jlmNWQueue.Dequeue().ToString();
                    }
                    jlmNWQueue.Clear();
                }

                // 温度3               
                counter = 0;
                var SheetWD3 = (MSExcel._Worksheet)shs.get_Item("温度三");
                if (SheetWD3 != null)
                {
                    while (jlmWD3Queue.Count > 0)
                    {
                        counter += 1;
                        SheetWD3.Cells[counter, 1] = timeArray[counter - 1];
                        SheetWD3.Cells[counter, 2] = jlmWD3Queue.Dequeue().ToString();
                    }
                    jlmWD3Queue.Clear();
                }

                // 温度4                
                counter = 0;
                var SheetWD4 = (MSExcel._Worksheet)shs.get_Item("温度四");
                if (SheetWD4 != null)
                {
                    while (jlmNWD4Queue.Count > 0)
                    {
                        counter += 1;
                        SheetWD4.Cells[counter, 1] = timeArray[counter - 1];
                        SheetWD4.Cells[counter, 2] = jlmNWD4Queue.Dequeue().ToString();
                    }
                    jlmNWD4Queue.Clear();
                }

                // 扭矩

                counter = 0;
                var SheetNJ = (MSExcel._Worksheet)shs.get_Item("扭矩");
                while (jlmNJQueue.Count > 0)
                {
                    counter += 1;
                    SheetNJ.Cells[counter, 1] = timeArray[counter - 1];
                    SheetNJ.Cells[counter, 2] = jlmNJQueue.Dequeue().ToString();
                    jlmNJQueue.Clear();
                }

                //屏蔽掉系统跳出的Alert
                app.AlertBeforeOverwriting = false;
                string saveFileName = (DateTime.Now.ToUniversalTime().Ticks / 10000000 - 62135596800).ToString() + ".xlsx";
                _wbk.SaveAs(Path.Combine(reportDir, saveFileName), Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, MSExcel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);
                app.Quit();
                //释放掉多余的excel进程
                Marshal.ReleaseComObject(app);
                app = null;

                DialogResult result = MessageBox.Show(this, "报告已保存，是否打开？", "保存询问", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                {
                    return;
                }
                System.Diagnostics.Process.Start(Path.Combine(reportDir, saveFileName));
            }
            catch (SystemException se)
            {
                Utils.WriteLog(this.GetType().ToString(), "SaveReport->" + se.Message);
                MessageBox.Show(se.ToString());
            }
        }

        private void genReport_Click(object sender, EventArgs e)
        {
            SaveReport();
        }

        private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!closeByBtn)
            {
                if (serialPortOpened)
                {
                    timerTask.Stop();
                    string cmd = "01 05 08 c0"; // m192
                    JLComm.ControlBtn(cmd, true);
                    jlmTaskService.Stop();
                }
                // 强制退出
                Environment.Exit(0);
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
        }
    }
}